#import "layer_game_background.h"

#import "mgr_game_mission.h"

#define BG_1_ACTION_STAND_TIME 1.0f
#define BG_1_ACTION_MOVE_TIME 2.0f

#define BG_1_STAR_FAD_TIME 1.0f
#define BG_1_CLOUD_MOVE_TIME 200.0f

#define BG_2_ACTION_STAND_TIME 1.0f
#define BG_2_ACTION_MOVE_TIME 2.0f

#define BG_3_ACTION_STAND_TIME 1.0f
#define BG_3_ACTION_MOVE_TIME 2.0f

@implementation LayerGameBackground

- (id)initLayerGameBackground:(int)Type
{
#if 0
	CCSequence *_SeqStar1;
	id _ActionFadeOutStar1;
	CCCallFuncN *_CallBackStar1FadOutOver;

	CCSequence *_SeqCloud;
	id _ActionMoveXCloud;
	CCCallFuncN *_CallBackCloudMoveOver;
#endif

	if ((self = [super init]))
	{
		/*load resource
		*/
		reLoadResource(&g_CfgGameBackgroundResourceTable.find(Type)->second);

		type_ = Type;

		switch (Type)
		{
		case BACKGROUND_TYPE_0:
			bg0ImageBackground_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg0ImagePath]];
			[self addChild:bg0ImageBackground_];
			bg0ImageBackground_.positionInPixels = g_ScrrenCenter;
			break;
		case BACKGROUND_TYPE_1:
			bg1ImageStar1_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg1ImageStar1Path]];
			[self addChild:bg1ImageStar1_ z:1];
			bg1ImageStar1_.positionInPixels = ccp(g_ScrrenCenter.x, g_CfgGameBg1StarStartY);
			bg1ImageStar2_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg1ImageStar2Path]];
			[self addChild:bg1ImageStar2_ z:1];
			bg1ImageStar2_.positionInPixels = ccp(g_ScrrenCenter.x, g_CfgGameBg1StarStartY);
			bg1ImageMoon_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg1ImageMoonPath]];
			[self addChild:bg1ImageMoon_ z:1];
			bg1ImageMoon_.positionInPixels = ccp(g_ScrrenCenter.x, g_CfgGameBg1MoonStartY);
			bg1ImageCloud_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg1ImageCloud]];
			[self addChild:bg1ImageCloud_ z:1];
			bg1ImageCloud_.positionInPixels = ccp(g_ScrrenCenter.x, g_CfgGameBg1CloudStartY);
			bg1ImageCastle_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg1ImageCastle]];
			[self addChild:bg1ImageCastle_ z:1];
			bg1ImageCastle_.positionInPixels = ccp(g_ScrrenCenter.x, g_CfgGameBg1CastleStartY);
#if 0
			bg1ImageStar2_.opacity = 0;
			_ActionFadeOutStar1 = [CCFadeOut actionWithDuration:BG_1_STAR_FAD_TIME];
			_CallBackStar1FadOutOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBg1Star1ActionOver:)];
			_SeqStar1 = [CCSequence actions:_ActionFadeOutStar1, _CallBackStar1FadOutOver, nil];
			[bg1ImageStar1_ runAction:_SeqStar1];

			_ActionMoveXCloud = [action_move_x_in_pixel actionWithDuration:BG_1_CLOUD_MOVE_TIME position:ccp(g_ScrrenSize.width + bg1ImageCloud_.contentSizeInPixels.width / 2, bg1ImageCloud_.positionInPixels.y)];
			_CallBackCloudMoveOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBg1CloudMoveOver:)];
			_SeqCloud = [CCSequence actions:_ActionMoveXCloud, nil];
			[bg1ImageCloud_ runAction:_SeqCloud];
#endif
			break;
		case BACKGROUND_TYPE_2:
			bg2ImageCloud_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg2ImageCloudPath]];
			[self addChild:bg2ImageCloud_ z:1];
			bg2ImageCloud_.positionInPixels = ccp(g_ScrrenCenter.x, g_CfgGameBg2CloudStartY);
			bg2ImageHill_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg2ImageHillPath]];
			[self addChild:bg2ImageHill_ z:1];
			bg2ImageHill_.positionInPixels = ccp(g_ScrrenCenter.x, g_CfgGameBg2HillStartY);
			break;
		case BACKGROUND_TYPE_3:
			/*image back*/
			bg3ImageBack_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg3ImageBackPath]];
			[self addChild:bg3ImageBack_];
			bg3ImageBack_.positionInPixels = g_ScrrenCenter;
			/*image middle*/
			bg3ImageMiddle_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg3ImageMiddlePath]];
			[self addChild:bg3ImageMiddle_];
			bg3ImageMiddle_.positionInPixels = g_CfgGameBg3MiddleStartPosition;
			/*image middle effect*/
			bg3ImageMiddleEffect_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg3ImageMiddleEffectPath]];
			[self addChild:bg3ImageMiddleEffect_];
			bg3ImageMiddleEffect_.positionInPixels = g_CfgGameBg3MiddleEffectStartPosition;
			/*image up left*/
			bg3ImageUpLeft_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg3ImageUpLeftPath]];
			[self addChild:bg3ImageUpLeft_];
			bg3ImageUpLeft_.positionInPixels = g_CfgGameBg3UpLeftStartPosition;
			/*image up right*/
			bg3ImageUpRight_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg3ImageUpRightPath]];
			[self addChild:bg3ImageUpRight_];
			bg3ImageUpRight_.positionInPixels = g_CfgGameBg3UpRightStartPosition;
			/*image down*/
			bg3ImageDown_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameBg3ImageDonwPath]];
			[self addChild:bg3ImageDown_];
			bg3ImageDown_.positionInPixels = g_CfgGameBg3DownStartPosition;
			break;
		default:;
		}
	}
	return self;
}

- (void)dealloc
{
	/*release resource
	*/
	reFreeResource(&g_CfgGameBackgroundResourceTable.find(type_)->second);

	[super dealloc];
}

- (void)tickBg1ActionPerviewStandTimeOver:(ccTime)Time
{
	[self unschedule:@selector(tickBg1ActionPerviewStandTimeOver:)];
	[self schedule:@selector(tickBg1ActionPreviewOver:) interval:BG_1_ACTION_MOVE_TIME];
	[bg1ImageStar1_ runAction:[action_move_y_in_pixel actionWithDuration:BG_1_ACTION_MOVE_TIME position:ccp(bg1ImageStar1_.positionInPixels.x, g_CfgGameBg1StarEndY)]];
	[bg1ImageStar2_ runAction:[action_move_y_in_pixel actionWithDuration:BG_1_ACTION_MOVE_TIME position:ccp(bg1ImageStar2_.positionInPixels.x, g_CfgGameBg1StarEndY)]];
	[bg1ImageMoon_ runAction:[action_move_y_in_pixel actionWithDuration:BG_1_ACTION_MOVE_TIME position:ccp(bg1ImageMoon_.positionInPixels.x, g_CfgGameBg1MoonEndY)]];
	[bg1ImageCloud_ runAction:[action_move_y_in_pixel actionWithDuration:BG_1_ACTION_MOVE_TIME position:ccp(bg1ImageCloud_.positionInPixels.x, g_CfgGameBg1CloudEndY)]];
	[bg1ImageCastle_ runAction:[action_move_y_in_pixel actionWithDuration:BG_1_ACTION_MOVE_TIME position:ccp(bg1ImageCastle_.positionInPixels.x, g_CfgGameBg1CastleEndY)]];
}

- (void)tickBg1ActionPreviewOver:(ccTime)Time
{
	[self unschedule:@selector(tickBg1ActionPreviewOver:)];
	CMgrGameMission::Instance()->SetMissionState(MISSION_STATE_SCENE_PERVIEW_OVER);
}

- (void)tickBg2ActionPerviewStandTimeOver:(ccTime)Time
{
	[self unschedule:@selector(tickBg2ActionPerviewStandTimeOver:)];
	[self schedule:@selector(tickBg2ActionPerviewOver:) interval:BG_1_ACTION_MOVE_TIME];
	[bg2ImageCloud_ runAction:[action_move_y_in_pixel actionWithDuration:BG_2_ACTION_MOVE_TIME position:ccp(bg2ImageCloud_.positionInPixels.x, g_CfgGameBg2CloudEndY)]];
	[bg2ImageHill_ runAction:[action_move_y_in_pixel actionWithDuration:BG_2_ACTION_MOVE_TIME position:ccp(bg2ImageHill_.positionInPixels.x, g_CfgGameBg2HillEndY)]];
}

- (void)tickBg2ActionPerviewOver:(ccTime)Time
{
	[self unschedule:@selector(tickBg2ActionPerviewOver:)];
	CMgrGameMission::Instance()->SetMissionState(MISSION_STATE_SCENE_PERVIEW_OVER);
}

- (void)tickBg3ActionPerviewStandTimeOver:(ccTime)Time
{
	[self unschedule:@selector(tickBg3ActionPerviewStandTimeOver:)];
	[self schedule:@selector(tickBg3ActionPerviewOver:) interval:BG_3_ACTION_MOVE_TIME];
	[bg3ImageUpLeft_ runAction:[action_move_in_pixel actionWithDuration:BG_3_ACTION_MOVE_TIME position:g_CfgGameBg3UpLeftEndPosition]];
	[bg3ImageUpRight_ runAction:[action_move_in_pixel actionWithDuration:BG_3_ACTION_MOVE_TIME position:g_CfgGameBg3UpRightEndPosition]];
	[bg3ImageDown_ runAction:[action_move_in_pixel actionWithDuration:BG_3_ACTION_MOVE_TIME position:g_CfgGameBg3DownEndPosition]];
	[bg3ImageMiddle_ runAction:[action_move_in_pixel actionWithDuration:BG_3_ACTION_MOVE_TIME position:g_CfgGameBg3MiddleEndPositio]];
	[bg3ImageMiddleEffect_ runAction:[action_move_in_pixel actionWithDuration:BG_3_ACTION_MOVE_TIME position:g_CfgGameBg3MiddleEffectEndPosition]];
}

- (void)tickBg3ActionPerviewOver:(ccTime)Time
{
	[self unschedule:@selector(tickBg3ActionPerviewOver:)];
	CMgrGameMission::Instance()->SetMissionState(MISSION_STATE_SCENE_PERVIEW_OVER);
}

- (void)draw
{
	UICenterRect Rect;
	switch (type_)
	{
	case BACKGROUND_TYPE_0: break;
	case BACKGROUND_TYPE_1:
		Rect.center = g_ScrrenCenter;
		Rect.size = g_ScrrenSize;
		glColor4ub(g_CfgGameBg1BackColor.r, g_CfgGameBg1BackColor.g, g_CfgGameBg1BackColor.b, 255);
		uiRenderRect(Rect);
		break;
	case BACKGROUND_TYPE_2:
		Rect.center = g_ScrrenCenter;
		Rect.size = g_ScrrenSize;
		glColor4ub(40, 87, 196, 255);
		uiRenderRect(Rect);
		break;
	default:;
	}
}

- (void)ActionMissionPreview
{
	switch (type_)
	{
	case BACKGROUND_TYPE_0: CMgrGameMission::Instance()->SetMissionState(MISSION_STATE_SCENE_PERVIEW_OVER); break;
	case BACKGROUND_TYPE_1:[self schedule:@selector(tickBg1ActionPerviewStandTimeOver:) interval:BG_1_ACTION_STAND_TIME]; break;
	case BACKGROUND_TYPE_2:[self schedule:@selector(tickBg2ActionPerviewStandTimeOver:) interval:BG_2_ACTION_STAND_TIME]; break;
	case BACKGROUND_TYPE_3:[self schedule:@selector(tickBg3ActionPerviewStandTimeOver:) interval:BG_3_ACTION_STAND_TIME]; break;
	default: break;
	}
}

- (void)CallBackBg1Star1ActionOver:(id)Sender
{
	CCSequence *_Seq;
	id _ActionFadeIn;
	id _ActionFadeOut;
	CCCallFuncN *_CallBackStar2FadeOver;

	_ActionFadeIn = [CCFadeIn actionWithDuration:BG_1_STAR_FAD_TIME];
	_ActionFadeOut = [CCFadeOut actionWithDuration:BG_1_STAR_FAD_TIME];
	_CallBackStar2FadeOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBg1Star2ActionOver:)];
	_Seq = [CCSequence actions:_ActionFadeIn, _ActionFadeOut, _CallBackStar2FadeOver, nil];
	[bg1ImageStar2_ runAction:_Seq];
}

- (void)CallBackBg1Star2ActionOver:(id)Sender
{
	CCSequence *_Seq;
	id _ActionFadeIn;
	id _ActionFadeOut;
	CCCallFuncN *_CallBackStar1FadeOver;

	_ActionFadeIn = [CCFadeIn actionWithDuration:BG_1_STAR_FAD_TIME];
	_ActionFadeOut = [CCFadeOut actionWithDuration:BG_1_STAR_FAD_TIME];
	_CallBackStar1FadeOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBg1Star1ActionOver:)];
	_Seq = [CCSequence actions:_ActionFadeIn, _ActionFadeOut, _CallBackStar1FadeOver, nil];
	[bg1ImageStar1_ runAction:_Seq];
}

- (void)CallBackBg1CloudMoveOver:(id)Sender
{
	CCSequence *_SeqCloud;
	id _ActionMoveXCloud;
	CCCallFuncN *_CallBackCloudMoveOver;

	[bg1ImageCloud_ setPositionInPixels:ccp(-bg1ImageCloud_.contentSizeInPixels.width / 2, bg1ImageCloud_.positionInPixels.y)];
	_ActionMoveXCloud = [action_move_x_in_pixel actionWithDuration:BG_1_CLOUD_MOVE_TIME position:ccp(g_ScrrenSize.width + bg1ImageCloud_.contentSizeInPixels.width / 2, bg1ImageCloud_.positionInPixels.y)];
	_CallBackCloudMoveOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackBg1CloudMoveOver:)];
	_SeqCloud = [CCSequence actions:_ActionMoveXCloud, nil];
	[bg1ImageCloud_ runAction:_SeqCloud];
}

@end